<?php  
	namespace Home\Controller;
	use Think\Controller;
	//教务人员管理
	//@author  程传民
	class TeacherController extends AllowController{
		public function index(){
			$users = D("users");
				
			$arr1 = array();
			$arr1['role_id'] = array("LT",8);		//角色id值要大于7的才是学员(班长，组长，学员)
			$arr2 = array('can_login'=>2);			//正常登陆的用户
			
			//按照姓名查找
			if(!empty($_GET['name'])){
				//按照角色查找
				if(!empty($_GET['role_id'])){
					$arr3 = array("role_id"=>$_GET['role_id']);
					$tot = $users->where($arr3)->where($arr2)->where("name like '%".$_GET['name']."%'")->count();
				}else{
					$tot = $users->where($arr1)->where($arr2)->where("name like '%".$_GET['name']."%'")->count();
				}
			}else{
				//按照角色查找
				if(!empty($_GET['role_id'])){
					$arr3 = array("role_id"=>$_GET['role_id']);
					$tot = $users->where($arr3)->where($arr2)->count();
				}else{
					$tot = $users->where($arr1)->where($arr2)->count();
				}
			}

			

			//分页管理
			$nums = 5;
	    	$page = new \Think\Page($tot,$nums);
	    	
	    	$search="";
			//按照姓名查找
			if(!empty($_GET['name'])){
				//按照角色查找
				if(!empty($_GET['role_id'])){
					$arr3 = array("role_id"=>$_GET['role_id']);
					$list = $users->where($arr3)->where($arr2)->where("name like '%".$_GET['name']."%'")->limit($page->firstRow,$page->listRows)->select();
					$search="/name/{$_GET['name']}/role_id/{$_GET['role_id']}";
				}else{
					$list = $users->where($arr1)->where($arr2)->where("name like '%".$_GET['name']."%'")->limit($page->firstRow,$page->listRows)->select();
					$search="/name/{$_GET['name']}";
				}
			}else{
				//按照角色查找
				if(!empty($_GET['role_id'])){
					$arr3 = array("role_id"=>$_GET['role_id']);
					$list = $users->where($arr3)->where($arr2)->limit($page->firstRow,$page->listRows)->select();
					$search="/role_id/{$_GET['role_id']}";
				}else{
					$list = $users->where($arr1)->where($arr2)->limit($page->firstRow,$page->listRows)->select();
				}
			}



			$role = D("role");
			$roles = $role->field("id,name")->where("id<=7")->select();
			$class = D("class");
			$classs = $class->select();
			$team = D("team");
			$teams = $team->select();
			foreach($list as $key=>$value){
				//角色
				foreach($roles as $k=>$v){
					if($list[$key]['role_id']==$v['id']){
						$list[$key]['role_id'] = $v['name'];
					}
				}
				//小组
				if($list[$key]['team']!=0){
					foreach($teams as $k=>$v){
						//	避免不同班重名小组
						if($list[$key]['class']==$v['class']){
							if($list[$key]['team']==$v['id']){
								$list[$key]['team'] = $v['name'];
							}
						}
					}
				}else{
					$list[$key]['team'] = "暂无小组";
				}
				//班级
				if($list[$key]['class']!=0){
					foreach($classs as $k=>$v){
						if($list[$key]['class']==$v['id']){
							$list[$key]['class'] = $v['name'];
						}
					}
				}else{
					$list[$key]['class'] = "暂无班级";
				}
				//状态
	 			// if($list[$key]['can_login']==2){
	 			$list[$key]['can_login']="正常登录";
	 			// }
			}


			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);
			$this->assign("roles",$roles);
			$this->assign("list",$list);
			$this->assign("search",$search);
			$this->display("Teacher/teacher");
		}

		public function add(){
			$role = D("role");
			$roles = $role->where("id <= 7")->select();
			$this->assign("roles",$roles);
			$this->display("Teacher/add");
		}

		public function insert(){
			$data['name'] = I("post.name");
			$data['password'] = md5(I("post.pass"));
			$data['can_login'] = I("post.canLogin");
			$data['role_id'] = I("post.roleid");

			$users = D("users");
			$info = $users->add($data);

			if($info){
				echo "添加成功";
			}else{
				echo "添加失败";
			}

		}

		//把用户禁用掉
		public function delete(){
			//users中的pid值
			$arr = array("id"=>I("post.id"));
			//把此用户的状态改为0  用户禁止登陆
			$a = array("can_login"=>0);
			$users = D("users");
			$info = $users->where($arr)->save($a);
			if($info){
				echo "成功";
			}else{
				echo "失败";
			}
		}

	}
?>